package cn.vconinfo.base.oss;

import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Base64;

@Slf4j
public class FileUtils {

    /**
     *  base64转换成文件
     *
     * @param base64
     * @param fileName
     * @param savePath
     */
    public static String base64ToFile(String base64, String fileName, String savePath) {
        File file = null;
        //创建文件目录
        String filePath = savePath;
        File dir = new File(filePath);
        if (!dir.exists() && !dir.isDirectory()) {
            dir.mkdirs();
        }
        BufferedOutputStream bos = null;
        java.io.FileOutputStream fos = null;
        try {
            byte[] bytes = Base64.getDecoder().decode(base64);
            file=new File(filePath, fileName);
            fos = new java.io.FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            log.error("写入文件出错", e);
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    log.error("文件关闭出错", e);
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    log.error("文件流出错", e);
                }
            }
        }

        return file.getPath();
    }

    /**
     *  根据URL地址下载文件到指定目录下面
     *
     * @param urlList
     * @param path
     */
    public static void downloadPicture(String urlList, String path) {
        URL url = null;
        DataInputStream dataInputStream = null;
        FileOutputStream fileOutputStream = null;
        ByteArrayOutputStream output = null;
        try {
            url = new URL(urlList);
            dataInputStream = new DataInputStream(url.openStream());

            fileOutputStream = new FileOutputStream(new File(path));
            output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            // 需要返回base64字符串时，开启下面的代码
//            BASE64Encoder encoder = new BASE64Encoder();
//            String encode = encoder.encode(buffer);//返回Base64编码过的字节数组字符串
//            System.out.println(encode);
            fileOutputStream.write(output.toByteArray());
        } catch (MalformedURLException e) {
            log.error("文件的URL解释出错", e);
        } catch (IOException e) {
            log.error("下载文件io异常", e);
        }finally {
            try {
                if(output != null){
                    output.close();
                    output = null;
                }
                if(fileOutputStream != null){
                    fileOutputStream.close();
                    fileOutputStream = null;
                }
                if(dataInputStream != null){
                    dataInputStream.close();
                    dataInputStream = null;
                }
            } catch (IOException e) {
                log.error("下载文件io异常", e);
            }
        }
    }


}
